package com.orphan.marketFront.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.orphan.marketFront.mapper.GoodsMapper;
import com.orphan.marketFront.mapper.OrderDetailMapper;
import com.orphan.marketFront.mapper.OrderMapper;
import com.orphan.marketFront.po.Order;
import com.orphan.marketFront.po.OrderDetail;
import com.orphan.marketFront.po.ReturnsPo;
import com.orphan.marketFront.service.IOrderService;
import com.orphan.marketFront.vo.OrderDetailVo;
import com.orphan.marketFront.vo.OrderReturnsVo;
import com.orphan.marketFront.vo.OrderVo;
import com.orphan.util.dto.ReturnsDetailsDto;
import com.orphan.util.dto.OrderDto;
import com.orphan.util.dto.OrderGoodsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements IOrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Override
    public List<OrderVo> listAll(long userId) {
        return orderMapper.listAll(userId);
    }

    @Override
    public List<OrderVo> queryByTime(String orderTime) {
        return orderMapper.queryByTime(orderTime);
    }

    @Override
    public int remove(String orderId) {
        return orderMapper.remove(orderId);
    }

    @Override
    public List<OrderDetailVo> orderDetailList(String orderNo) {
        return orderMapper.orderDetailList(orderNo);
    }

    @Override
    public OrderReturnsVo orderReturns(String orderNo, String goodsName) {
        return orderMapper.orderReturns(orderNo,goodsName);
    }

    @Override
    public int addOrderReturns(String orderId, String returnsNo, Date returnsTime) {
        return orderMapper.addOrderReturns(orderId,returnsNo,returnsTime);
    }

    @Override
    public List<ReturnsPo> getReturnsId(String orderId) {
        return orderMapper.getReturnsId(orderId);
    }

    @Override
    public int addReturnsDetails(Long orderReturnsId, String goodsType, String goodsName, Double goodsPrice, int goodsCount, int goodsOrderStatus) {
        return orderMapper.addReturnsDetails(orderReturnsId,goodsType,goodsName,goodsPrice,goodsCount,goodsOrderStatus);
    }

    @Override
    public int updateStatus(int goodsOrderStatus,String goodsName) {
        return orderMapper.updateStatus(goodsOrderStatus,goodsName);
    }

    @Override
    public List<ReturnsDetailsDto> returnsDetails(String orderId) {
        return orderMapper.returnsDetails(orderId);
    }

    @Override
    public Order selectOrderById(String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        Order order = orderMapper.selectOne(wrapper);
        return order;
    }

    /**
     * 查询订单商品明细
     * @param orderId
     * @return
     */
    public List<OrderDetail> selectOrderDetailByOrderId(String orderId) {
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        List<OrderDetail> orderDetailList = orderDetailMapper.selectList(wrapper);
        return orderDetailList;
    }


    /**
     * 根据订单id修改订单支付状态
     * @param orderId
     * @param payStatus
     * @return
     */
    public Integer updateOrderPayStatus(String orderId,Integer payStatus) {
        int result = orderMapper.updateOrderPayStatus(orderId,payStatus);
        return result;
    }

    /**
     * 新增订单
     * @param orderDto 新的订单
     * @return
     */
    public Integer insertOrder(OrderDto orderDto) {
        Order order = tableOrderParse(orderDto);
        int result = orderMapper.insert(order);
        return result;
    }

    /**
     * 将订单中的商品持久化到数据库
     * @param orderDto
     * @return
     */
    public Integer insertOrderDetail(OrderDto orderDto) {
        List<OrderDetail> orderDetails = tableOrderDetailParse(orderDto);
        Integer num = 0;
        for (int i = 0; i < orderDetails.size(); i++) {
            OrderDetail orderDetail = orderDetails.get(i);
            int result = orderDetailMapper.insert(orderDetail);
            num = num + result;
        }
        return num;
    }

    /**
     * 将传输的订单表中的订单信息转化为订单表的po
     * @param orderDto
     * @return
     */
    public Order tableOrderParse(OrderDto orderDto){
        Order order = new Order();
        order.setOrderId(orderDto.getOrderId());
        order.setUserId(orderDto.getUserId());
        order.setConsignee(orderDto.getConsignee());
        order.setConsigneeTel(orderDto.getConsigneeTel());
        order.setOrderAddress(orderDto.getOrderAddress());
        order.setOrderStatus(orderDto.getOrderStatus());
        order.setOrderTime(orderDto.getOrderTime());
        order.setPayMethod(orderDto.getPayMethod());
        order.setPayStatus(orderDto.getPayStatus());
        order.setTotalAmount(orderDto.getTotalAmount());
        return order;
    }

    /**
     * 将数据库订单表中的订单信息转化为传输用的订单表
     * @param order
     * @return
     */
    public OrderDto orderDtoParse(Order order,List<OrderDetail> OrderDetailList){
        OrderDto orderDto = new OrderDto();
        orderDto.setOrderId(order.getOrderId());
        orderDto.setUserId(order.getUserId());
        orderDto.setConsignee(order.getConsignee());
        orderDto.setConsigneeTel(order.getConsigneeTel());
        orderDto.setOrderAddress(order.getOrderAddress());
        orderDto.setOrderStatus(order.getOrderStatus());
        orderDto.setOrderTime(order.getOrderTime());
        orderDto.setPayMethod(order.getPayMethod());
        orderDto.setPayStatus(order.getPayStatus());
        orderDto.setTotalAmount(order.getTotalAmount());

        ArrayList<OrderGoodsDto> list = new ArrayList<>();
        if(OrderDetailList != null) {
            for (int i = 0; i < OrderDetailList.size(); i++) {
                OrderDetail orderDetail = OrderDetailList.get(i);
                OrderGoodsDto orderGoodsDto = new OrderGoodsDto();
                orderGoodsDto.setOrderId(orderDetail.getOrderId());
                orderGoodsDto.setGoodsName(orderDetail.getGoodsName());
                orderGoodsDto.setGoodsCount(orderDetail.getGoodsCount());
                orderGoodsDto.setGoodsPrice(orderDetail.getGoodsPrice());
                orderGoodsDto.setGoodsType(orderDetail.getGoodsType());
                orderGoodsDto.setGoodsPicture(orderDetail.getGoodsPicture());
                orderGoodsDto.setGoodsOrderStatus(orderDetail.getGoodsOrderStatus());
                list.add(orderGoodsDto);
            }
        }
        orderDto.setGoodsList(list);

        return orderDto;
    }

    /**
     * 将传输的订单表中的商品明细转化为订单详情表的po
     * @param orderDto 传输的订单表
     * @return 订单详情po
     */
    public List<OrderDetail> tableOrderDetailParse(OrderDto orderDto){
        ArrayList<OrderDetail> list = new ArrayList<>();
        List<OrderGoodsDto> dtoList = orderDto.getGoodsList();
        if(dtoList != null) {
            for (int i = 0; i < dtoList.size(); i++) {
                OrderGoodsDto goodsDto = dtoList.get(i);
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setOrderId(goodsDto.getOrderId());
                orderDetail.setGoodsName(goodsDto.getGoodsName());
                orderDetail.setGoodsCount(goodsDto.getGoodsCount());
                orderDetail.setGoodsPrice(goodsDto.getGoodsPrice());
                orderDetail.setGoodsType(goodsDto.getGoodsType());
                orderDetail.setGoodsPicture(goodsDto.getGoodsPicture());
                orderDetail.setGoodsOrderStatus(goodsDto.getGoodsOrderStatus());
                list.add(orderDetail);
            }
        }
        return list;
    }


}
